package com.hsh.servlet.user;/**
 * @author xrkhy
 * @date 2025/6/28 23:33
 * @description
 */

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.TypeReference;
import com.hsh.pojo.Page;
import com.hsh.pojo.User;
import com.hsh.service.UserService;
import com.hsh.service.impl.UserServiceImpl;
import com.hsh.utils.JsonResultUtil;
import jakarta.servlet.*;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;

import java.io.IOException;

@WebServlet(name = "UserPage", value = "/UserPage")
public class UserPage extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        // String json = {"currentPage":1,"pageSize":5,"total":10,"obj":{"id":"1","userName":"xrkhy","password":null,"role":null,"age":null}}
        String json =request.getReader().readLine();
        System.out.println(json);
        // 当
//        json = JsonUtils.processCondition(json);
//        System.out.println(json);
//        Page<User> page = JSONObject.parseObject(json, Page.class);
        Page<User> page = JSON.parseObject(json, new TypeReference<Page<User>>() {});

//        System.out.println(page);
//        System.out.println(page.getCondition().toString());
//        System.out.println(page.getCondition().getUserName());

        UserService userService = new UserServiceImpl();
        Page<User> respage = userService.pageQuery(page);
        System.out.println("响应结果");
        System.out.println(respage);
        String jsonMassage = JsonResultUtil.buildResult(200, "成功", respage);
        response.getWriter().write(jsonMassage);

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}
